#!/bin/sh --

# Copyright (c) 2019-2020 Qualcomm Technologies, Inc.
# All Rights Reserved.
# Confidential and Proprietary - Qualcomm Technologies, Inc.

ril_db="/data/vendor/radio/qcrilNr.db"

help() {
    echo "Syntax error! Example: qtigetprop [config_name]"
}

if [ $# -gt 1 ]; then
    help
    exit 1
elif [ $# -eq 1 ]; then
    prop_name=$1
fi

# Disable exit on non 0
set +e

if [[ ! -f $ril_db ]]; then
    getprop "$@"
else
    if [ ! -z "$prop_name" ]; then
        query_result=`echo "SELECT 1 FROM qcril_properties_table WHERE property='$prop_name';" | sqlite3 $ril_db`
        if [[ $query_result == "1" ]]; then
            result=`echo "SELECT value FROM qcril_properties_table WHERE property='$1';" | sqlite3 $ril_db`
            if [ "$result" == "" ]; then
                echo "SELECT def_val FROM qcril_properties_table WHERE property='$1';" | sqlite3 $ril_db
            else
                echo $result
            fi
        else
            getprop $prop_name
        fi
    else
        # print RIL configs
        echo "SELECT * FROM qcril_properties_table;" | sqlite3 $ril_db | awk -F '|' '{
            if ($3=="")
                print "["$1"]: ["$2"]";
            else
                print "["$1"]: ["$3"]";
        }'
        # print Android properties
        getprop 2>/dev/null || true
    fi
fi
